import useAntdList from "@/hooks/useAntdList"
import useAntdModal, { AntdModalProps } from "@/hooks/useAntdModal"
import { queryCodeupReleasePlan } from "@/service/deliver"
import { formatDate } from "@/utils/utils"
import { Button, Drawer, Space, Table } from "antd"
import { useEffect } from "react"
import DeployApp from "./DeployApp"

const DeployDrawer = ({ modalProps, payload }: AntdModalProps) => {
    console.log('DeployDrawer')
    const { tableProps, run } = useAntdList(() => queryCodeupReleasePlan({ id: payload.id }), {
        manual: true
    })
    const deployApp = useAntdModal();
    useEffect(() => {
        if (modalProps.open) {
            //   @ts-ignore
            run()
        }
    }, [modalProps.open])
    const onDeploy = (_) => {
        deployApp.openModal({
            iterationId:_.id,
            env:payload.env,
            id:_.appId,
            enName:_.appEnName,
            version:_.version,
        },{title:'部署'})
        // console.log({env,..._})
    }
    return <Drawer
        width={600}
        extra={
            <Space>
                <Button onClick={modalProps.onCancel}>Cancel</Button>
                <Button type="primary" onClick={modalProps.onOk}>
                    OK
                </Button>
            </Space>
        }

        {...modalProps}>
        <DeployApp {...deployApp} />
        <Table rowKey='id' {...tableProps} size='small' columns={[{
            dataIndex: 'name',
            title: '发布计划名称'
        }, {
            title: '版本',
            dataIndex: 'version',
            // render(_) {
            //     return `release/${_}`
            // }
        }, {
            dataIndex: 'createdAt',
            title: '创建时间',
            render: (_) => formatDate(_)

        }, {
            title: '操作',
            render(_) {
                return <Space>
                    <Button onClick={() => onDeploy(_)} type='link'>部署</Button>
                    {/* <Button type='link'>切换版本</Button> */}
                </Space>
            }
        }]} />
    </Drawer>
}
export default DeployDrawer